package com.lib.service.impl;

import com.lib.common.properties.JwtProperties;
import com.lib.common.utils.JwtUtils;
import com.lib.mapper.LoginMapper;
import com.lib.pojo.entity.AdminUser;
import com.lib.pojo.entity.LoginInfo;
import com.lib.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private JwtProperties jwtProperties;

    //1:管理员登录
    @Override
    public LoginInfo login(AdminUser adminUser) {
        //1：获取用户数据
        AdminUser u = loginMapper.selectUsernameOrPassword(adminUser);
        //2：判断用户数据是否存在
        if (u != null){
            Map<String, Object> jwtData = new HashMap<>();
            jwtData.put("id",u.getId());
            jwtData.put("username",u.getUsername());
            String token = JwtUtils.generateToken(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(),jwtData);
            return new LoginInfo(u.getId(),u.getUsername(),token);
        }
        //用户不存在返回空
        return null;
    }
}
